package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Visit;
import com.cloudwise.gproject.service.VisitService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * (Visit)表控制层
 *
 * @author makejava
 * @since 2021-03-23 18:59:07
 */
@Controller
@RequestMapping("visit")
public class VisitController {
    /**
     * 服务对象
     */
    @Resource
    private VisitService visitService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.visitService.queryById(id));
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toVisit")
    public String toVisit() {
        return "back/visit";
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Visit visit) {
        if (visit.getStartTime() ==null || visit.getStartTime()==""){
            visit.setStartTime("2000-01-01 00:00:00");
        }
        if (visit.getEndTime()=="" ||  visit.getEndTime()==null){
            visit.setEndTime("2100-01-01 00:00:00");
        }
        PageInfo pageInfo = visitService.queryAll(page, limit, visit);
        System.out.println(pageInfo.getList());
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param visit 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Visit visit) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.visitService.insert(visit));
    }

    /**
     * 修改数据
     *
     * @param visit 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Visit visit, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        visit.setUpdateBy(username);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.visitService.update(visit));
    }

    /**
     * 通过主键删除数据
     *
     * @param visitId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer visitId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.visitService.deleteById(visitId));
    }

    /**
     * 导入Excel
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("importVisit")
    public Result importVisit(@RequestParam MultipartFile file) {
        System.out.println("importVisit方法...");
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                visitService.importVisit(file));
    }

    /**
     * 通过实体作为筛选条件查询
     *到处Excel
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("exportVisit")
    public Result exportVisit(Visit visit) {
        System.out.println("exportVisit进来了llllllllllllllllllll");
        System.out.println(visitService.exportVisit(visit));
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                0,visitService.exportVisit(visit));
    }

    @ResponseBody
    @RequestMapping("visitEchartTime")
    public Result visitEchartTime(String loginName,HttpServletRequest request){
        if (loginName==null||loginName==""){
            loginName = (String)request.getSession().getAttribute("username");
        }
        Map<String, Object> map = visitService.visitEchartTime(loginName);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                map);

    }

    @ResponseBody
    @RequestMapping("visitEchartName")
    public Result visitEchartName(String startTime,String endTime){
        if (startTime == null || "".equals(startTime)) {
            startTime = "2000-01-01";
        }
        if (endTime == null || "".equals(endTime)) {
            endTime = "2100-01-01";
        }
        Map<String, Object> map = visitService.visitEchartName(startTime, endTime);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                map);

    }
}
